package com.mycompony.shopping.shangguan.sys.service.impl;

import java.util.Date;
import java.util.List;

import org.apache.commons.lang.StringUtils;

import com.mycompony.shopping.shangguan.sys.dao.PasswordMapper;
import com.mycompony.shopping.shangguan.sys.model.Password;
import com.mycompony.shopping.shangguan.sys.model.PasswordExample;
import com.mycompony.shopping.shangguan.sys.service.PasswordService;

public class PasswordServiceImpl implements PasswordService {

	private PasswordMapper passwordMapper;

	public PasswordMapper getPasswordMapper() {
		return passwordMapper;
	}

	public void setPasswordMapper(PasswordMapper passwordMapper) {
		this.passwordMapper = passwordMapper;
	}

	public Password validatePassword(Password password) {
		if (password == null || StringUtils.isEmpty(password.getPassword())
				|| StringUtils.isEmpty(password.getUserid())) {
			return null;
		}

		Date now = new Date();

		PasswordExample passwordExample = new PasswordExample();

		passwordExample.createCriteria().andUseridEqualTo(password.getUserid())
				.andPasswordEqualTo(password.getPassword())
				.andExpirydateGreaterThan(now);

		List<Password> list = passwordMapper.selectByExample(passwordExample);

		if (list == null || list.isEmpty() || list.size() > 1) {
			return null;
		}

		return list.get(0);
	}

}
